﻿
var HttPRequest = false;

function loadContent(url) {
    init_ajax();

    var pmeters = "";

    HttPRequest.open('POST',url+".php",true);
    HttPRequest.setRequestHeader("Content-type", "application/x-www-form-urlencoded;charset=utf-8");
    HttPRequest.setRequestHeader("Content-length", pmeters.length);
    HttPRequest.setRequestHeader("Connection", "close");
    HttPRequest.send(pmeters);

    document.getElementById("imgLoading").style.display = '';
    document.getElementById("main-content").style.display = 'none';

    HttPRequest.onreadystatechange = function(){
        if(HttPRequest.readyState == 3){  // Loading Request
            document.getElementById("imgLoading").style.display = '';
            document.getElementById("main-content").style.display = 'none';
        }

        if(HttPRequest.readyState == 4){ // Return Request
            document.getElementById("imgLoading").style.display = 'none';
            document.getElementById("main-content").style.display = '';
            document.getElementById("main-content").innerHTML = HttPRequest.responseText;
        }
    }
}

function loadContent2(URL,PMETERS,ID) {
    init_ajax();

    var url = URL + ".php";
	var pmeters = PMETERS + "=" + encodeURI(ID);

    HttPRequest.open('POST',url,true);
    HttPRequest.setRequestHeader("Content-type", "application/x-www-form-urlencoded;charset=utf-8");
    HttPRequest.setRequestHeader("Content-length", pmeters.length);
    HttPRequest.setRequestHeader("Connection", "close");
    HttPRequest.send(pmeters);

    document.getElementById("imgLoading").style.display = '';
    document.getElementById("main-content").style.display = 'none';

    HttPRequest.onreadystatechange = function(){
        if(HttPRequest.readyState == 3){  // Loading Request
            document.getElementById("imgLoading").style.display = '';
            document.getElementById("main-content").style.display = 'none';
        }

        if(HttPRequest.readyState == 4){ // Return Request
            document.getElementById("imgLoading").style.display = 'none';
            document.getElementById("main-content").style.display = '';
            document.getElementById("main-content").innerHTML = HttPRequest.responseText;
        }
    }
}

function loadContent3(URL,PMETERS1,ID1,PMETERS2,ID2) {
    init_ajax();

    var url = URL + ".php";
	var pmeters = PMETERS1 + "=" + encodeURI(ID1) + "&" +PMETERS2 + "=" + encodeURI(ID2);

    HttPRequest.open('POST',url,true);
    HttPRequest.setRequestHeader("Content-type", "application/x-www-form-urlencoded;charset=utf-8");
    HttPRequest.setRequestHeader("Content-length", pmeters.length);
    HttPRequest.setRequestHeader("Connection", "close");
    HttPRequest.send(pmeters);

    document.getElementById("imgLoading").style.display = '';
    document.getElementById("main-content").style.display = 'none';

    HttPRequest.onreadystatechange = function(){
        if(HttPRequest.readyState == 3){ // Loading Request
            document.getElementById("imgLoading").style.display = '';
            document.getElementById("main-content").style.display = 'none';
        }

        if(HttPRequest.readyState == 4){ // Return Request
            document.getElementById("imgLoading").style.display = 'none';
            document.getElementById("main-content").style.display = '';
            document.getElementById("main-content").innerHTML = HttPRequest.responseText;
        }
    }
}

function loadProduct(PID) {
    init_ajax();

    var url = 'pro_detail.php';
	var pmeters = "pro_id=" + encodeURI(PID);

    HttPRequest.open('POST',url,true);
    HttPRequest.setRequestHeader("Content-type", "application/x-www-form-urlencoded;charset=utf-8");
    HttPRequest.setRequestHeader("Content-length", pmeters.length);
    HttPRequest.setRequestHeader("Connection", "close");
    HttPRequest.send(pmeters);

    document.getElementById("imgLoading").style.display = '';
    document.getElementById("main-content").style.display = 'none';

    HttPRequest.onreadystatechange = function(){
        if(HttPRequest.readyState == 3){  // Loading Request
            document.getElementById("imgLoading").style.display = '';
            document.getElementById("main-content").style.display = 'none';
        }

        if(HttPRequest.readyState == 4){ // Return Request
            document.getElementById("imgLoading").style.display = 'none';
            document.getElementById("main-content").style.display = '';
            document.getElementById("main-content").innerHTML = HttPRequest.responseText;
        }
    }
}

function loadPage(URL,PMETERS,pageid) {
    init_ajax();

    var url = URL + ".php";
	var pmeters = PMETERS + "=" + encodeURI(pageid);

    HttPRequest.open('POST',url,true);
    HttPRequest.setRequestHeader("Content-type", "application/x-www-form-urlencoded;charset=utf-8");
    HttPRequest.setRequestHeader("Content-length", pmeters.length);
    HttPRequest.setRequestHeader("Connection", "close");
    HttPRequest.send(pmeters);

    document.getElementById("imgLoading").style.display = '';
    document.getElementById("main-content").style.display = 'none';

    HttPRequest.onreadystatechange = function(){
        if(HttPRequest.readyState == 3){  // Loading Request
            document.getElementById("imgLoading").style.display = '';
            document.getElementById("main-content").style.display = 'none';
        }

        if(HttPRequest.readyState == 4){ // Return Request
            document.getElementById("imgLoading").style.display = 'none';
            document.getElementById("main-content").style.display = '';
            document.getElementById("main-content").innerHTML = HttPRequest.responseText;
        }
    }
}

function checkUser(URL,PMETERS,ID) {
    init_ajax();

    var url = URL + ".php";
	var pmeters = PMETERS + "=" + encodeURI(ID);

    HttPRequest.open('POST',url,true);
    HttPRequest.setRequestHeader("Content-type", "application/x-www-form-urlencoded;charset=utf-8");
    HttPRequest.setRequestHeader("Content-length", pmeters.length);
    HttPRequest.setRequestHeader("Connection", "close");
    HttPRequest.send(pmeters);

    HttPRequest.onreadystatechange = function(){
        if(HttPRequest.readyState == 3){  // Loading Request
            document.getElementById("mySpans").style.display = 'none';
        }

        if(HttPRequest.readyState == 4){ // Return Request
            document.getElementById("mySpans").innerHTML = HttPRequest.responseText;
        }
    }
}

function addTopic(URL,PMETERS,ID,ID2,ID3) {
    init_ajax();

    var url = URL + ".php";
	var pmeters = PMETERS + "=" + encodeURI(ID) + "&topic=" + encodeURI(ID2) + "&tdetail=" +  encodeURI(ID3);

    HttPRequest.open('POST',url,true);
    HttPRequest.setRequestHeader("Content-type", "application/x-www-form-urlencoded;charset=utf-8");
    HttPRequest.setRequestHeader("Content-length", pmeters.length);
    HttPRequest.setRequestHeader("Connection", "close");
    HttPRequest.send(pmeters);

    HttPRequest.onreadystatechange = function(){
        if(HttPRequest.readyState == 3){  // Loading Request
            document.getElementById("main-content").style.display = 'none';
        }

        if(HttPRequest.readyState == 4){ // Return Request
            document.getElementById("main-content").innerHTML = HttPRequest.responseText;
            document.getElementById('topic').value = '';
            document.getElementById('tdetail').value = '';
        }
    }
}

function init_ajax() {
    HttPRequest = false;
    if (window.XMLHttpRequest) { // Mozilla, Safari,...
  	 HttPRequest = new XMLHttpRequest();
  	 if (HttPRequest.overrideMimeType) {
  		HttPRequest.overrideMimeType('text/html');
  	 }
    } else if (window.ActiveXObject) { // IE
  	 try {
  		HttPRequest = new ActiveXObject("Msxml2.XMLHTTP");
  	 } catch (e) {
  		try {
  		   HttPRequest = new ActiveXObject("Microsoft.XMLHTTP");
  		} catch (e) {}
  	 }
    }

    if (!HttPRequest) {
  	 alert('Cannot create XMLHTTP instance');
  	 return false;
    }
}
